/**
 * Copyright (c) 2012-2013 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners.
 * See LICENSE.TXT for license information.
 */

package com.nokia.example.amaze.sensors;

public abstract class AccelerationProvider {

    private Listener listener;

    /**
     * Callback for subclasses
     * @param ax
     * @param ay
     * @param az 
     */
    protected final void dataReceived(double ax, double ay, double az) {
        if (listener != null) {
            listener.onDataReceived(ax, ay, az);
        }
    }

    /**
     * Closes the sensor connection
     */
    public abstract void close();

    public static AccelerationProvider getProvider(Listener listener) {
        AccelerationProvider provider = null;
        try {
            if (sensorsSupported()) {
                Class c = Class.forName("com.nokia.example.amaze.sensors."
                        + "AccelerationProviderImpl");
                provider = (AccelerationProvider) c.newInstance();
                provider.listener = listener;
            }
        }
        catch (Exception e) {
            System.out.println("Failed to create acceleration provider instance!");
        }
        
        return provider;
    }

    public static boolean sensorsSupported() {
        try {
            Class.forName("javax.microedition.sensor.SensorConnection");
            return true;
        }
        catch (Exception e) {
            return false;
        }
    }
    
    public interface Listener {
        void onDataReceived(double ax, double ay, double az);
    }    
}
